#include	"lp_global_vars.h"
#include 	"config_parser.h"

char proposerID_char[STRING_SIZE];

//------------------------------------------------------------------------------
void p_initialize() {
	sprintf(proposerID_char, "%d", proposerID);
}
//------------------------------------------------------------------------------
int generate_unique_id() {
	int temp;
	char stringTemp[STRING_SIZE];
	digit++;
	temp = digit * 10;
	sprintf(stringTemp, "%d", temp);
	int new_msg_id = atoi(strcat(stringTemp, proposerID_char));
	return new_msg_id;

}
//------------------------------------------------------------------------------
void send_proposer_msg_to_successor(int next_index_to_proposer) {

	p_msgs[proposerID][next_index_to_proposer].mili_seconds = getMiliSeconds();
	int status = send((get_config_info()->learner_infos[get_config_info()->self_learner_id].successor_sid), &(p_msgs[proposerID][next_index_to_proposer]), sizeof(proposer_msg), MSG_WAITALL);
	//	printf("%d\n", status);
	assert(status == sizeof(proposer_msg));

	lp_interval_performance->interval_bytes_proposed += sizeof(proposer_msg) - PROPOSER_MSG_HEADER;

}
//------------------------------------------------------------------------------
void generate_proposer_msg_timer_event(int sd, short event, void * arg) {

	UNUSED_ARG(sd);
	UNUSED_ARG(event);
	UNUSED_ARG(arg);

	int pmsg_counter = 0;
	lp_interval_performance->interval_num_entrance++;
	learner_deliver();
	//printf("PROPOSEING	%d < %d	prop\n", lp_interval_performance->next_location_index_in_proposer, INTERNAL_BUFFER_SIZE + (lp_interval_performance->highest_deliverable_inst_id /get_config_info()->total_num_proposers)- 1);

	while (pmsg_counter < SUBMIT_PER_WAKE) {
		if ((lp_interval_performance->next_location_index_in_proposer) < (INTERNAL_BUFFER_SIZE + (lp_interval_performance->highest_self_deliverable) - 1)) {

			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].type = 'p';
			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].msg_id = generate_unique_id();
			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].proposer_id = proposerID;
			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].highest_deliverable_inst_id = lp_interval_performance->highest_deliverable_inst_id;
			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].location_index_in_proposer = lp_interval_performance->next_location_index_in_proposer;
			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].num_nodes_visited = 1;
			p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].final_decision = -1;

			send_proposer_msg_to_successor((lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE);
			//printf("%d	HD: %d\n", p_msgs[proposerID][(lp_interval_performance->next_location_index_in_proposer) % INTERNAL_BUFFER_SIZE].msg_id, lp_interval_performance->highest_deliverable_inst_id);

			lp_interval_performance->interval_num_packets_proposed++;
			lp_interval_performance->total_num_packets_proposed++;
			lp_interval_performance->next_location_index_in_proposer++;
			pmsg_counter++;
		} else
			break;
	}

	evtimer_add(timeoutEv2, &tv2);
}

